class ApiService {
  constructor() {
    this.baseURL = 'https://your-api-domain.com/api'
  }
  
  // 统一请求方法
  async request(url, options = {}) {
    const token = uni.getStorageSync('token')
    const config = {
      url: `${this.baseURL}${url}`,
      header: {
        'Authorization': `Bearer ${token}`,
        'Content-Type': 'application/json'
      },
      ...options
    }
    
    return new Promise((resolve, reject) => {
      uni.request({
        ...config,
        success: (res) => {
          if (res.statusCode === 200) {
            resolve(res.data)
          } else {
            reject(res)
          }
        },
        fail: reject
      })
    })
  }
  
  // 获取推荐图书
  getRecommendedBooks() {
    return this.request('/books/recommended')
  }
  
  // 获取新书
  getNewBooks() {
    return this.request('/books/new')
  }
  
  // 获取热销图书
  getHotBooks() {
    return this.request('/books/hot')
  }
  
  // 获取图书详情
  getBookDetail(id) {
    return this.request(`/books/${id}`)
  }
  
  // 数据采集接口
  async scrapeBooks() {
    return this.request('/scrape/books', { method: 'POST' })
  }
}

export default new ApiService()